package servlet.initparam;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.domain.Member;

/**
 * Servlet implementation class GreetingsServlet
 */
public class GreetingsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//		application scope에서 admin 이름으로 binding된 속성 조회
		ServletContext ctx = getServletContext();
		Member admin = (Member)ctx.getAttribute("admin");
		System.out.println("GreetingServlet : admin :" + admin);
		
		//요청 파라미터 조회
		//1. 요청정보 한글처리
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		//2. 초기 파라미터 조회
		ServletConfig conf = getServletConfig();
		String greeting1 = conf.getInitParameter("greeting");
		//위에 방법이나 아래에 방법이나 같은데 아래가 편해서 더 많이 쓴다.
		String greeting2 = getInitParameter("greeting2");
		//2-2 application단위의 초기파라미터 조회
		//ServletContext 객체 조회
		ServletContext context = getServletContext();//서블릿컨텍스트객체가 꼭 필요하다.
		String adminEmail = context.getInitParameter("admin email");
		String companyAddress = context.getInitParameter("company address");
		
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		//name : 요청파라미터, greeting1/2 : 초기파라미터
		out.println(name+"님 "+greeting1+greeting2);
		out.println("<hr>관리자 이메일 : "+ adminEmail+", 회사주소 : "+companyAddress);
		out.println("</body></html>");
	}

}
